package com.bulletelc.apollopre.module.main.classify;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.Classify;
import com.bulletelc.apollopre.framework.MVPBaseFragment;
import com.bulletelc.apollopre.module.display.product.resultList.ResultListActivity;
import com.bulletelc.apollopre.module.display.search.SearchActivity;
import com.bulletelc.apollopre.module.richscan.RichScanActivity;
import com.bulletelc.apollopre.utils.MyUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * author: lhl
 * time  : 2017/10/15
 * desc  : 分类界面
 */

public class ClassifyFragment extends MVPBaseFragment<ClassifyContract.View,ClassifyPresenter> implements ClassifyContract.View,View.OnClickListener,
        AdapterView.OnItemClickListener, ExpandableListView.OnChildClickListener, ExpandableListView.OnGroupClickListener,
        ExpandableListView.OnGroupExpandListener {

    // views
    private LinearLayout llRichScan;
    private TextView tvSearch;
    private ListView lvClassifyFirst;
    private ExpandableListView elvClassifySecond;
    private ProgressBar pbLoad;

    // data
    private List<Classify> classifyFirst;
    private ClassifyFirstAdapter firstAdapter;
    private List<Classify> classifySecond;
    private ClassifySecondAdapter secondAdapter;
    private boolean first = true;

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            return;
        } else {
            if (first) {
                MyUtil.setProgressBar(pbLoad, true);
                presenter.getClassifyFirst();
            }
        }
    }

    @Override
    public int getLayoutRes() {
        return R.layout.classify_fragment;
    }

    @Override
    public void assignViews() {
        llRichScan = findView(R.id.llRichScan);
        tvSearch = findView(R.id.tvSearch);
        lvClassifyFirst = findView(R.id.lvClassifyFirst);
        elvClassifySecond = findView(R.id.elvClassifySecond);
        pbLoad = findView(R.id.pbLoad);
    }

    @Override
    public void setListener() {
        tvSearch.setOnClickListener(this);
        llRichScan.setOnClickListener(this);
        lvClassifyFirst.setOnItemClickListener(this);
        elvClassifySecond.setOnItemClickListener(this);
        elvClassifySecond.setOnChildClickListener(this);
        elvClassifySecond.setOnGroupClickListener(this);
        elvClassifySecond.setOnGroupExpandListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        // 一级分类
        classifyFirst = new ArrayList<>();
        firstAdapter = new ClassifyFirstAdapter(baseActivity,classifyFirst);
        lvClassifyFirst.setAdapter(firstAdapter);

        // 二级分类
        classifySecond = new ArrayList<>();
        secondAdapter = new ClassifySecondAdapter(baseActivity,classifySecond);
        elvClassifySecond.setAdapter(secondAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvSearch:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.llRichScan:
                startActivity(new Intent(getActivity(), RichScanActivity.class));
                break;
        }
    }

    @Override
    protected ClassifyPresenter createPresenter() {
        return new ClassifyPresenter();
    }

    @Override
    public void onSuccessFirst(List<Classify> classifyList) {
        this.classifyFirst.clear();
        this.classifyFirst.addAll(classifyList);
        firstAdapter.notifyDataSetChanged();
        presenter.getClassifySecond(classifyFirst.get(0).getId());
        MyUtil.setProgressBar(pbLoad, false);
        first = false;
    }

    @Override
    public void onFailFirst(String fail) {
        Toast.makeText(baseActivity, fail, Toast.LENGTH_SHORT).show();
        MyUtil.setProgressBar(pbLoad, false);
    }

    @Override
    public void onErrorFirst(String error) {
        Toast.makeText(baseActivity, R.string.network_msg, Toast.LENGTH_SHORT).show();
        MyUtil.setProgressBar(pbLoad, false);
    }

    @Override
    public void successSecond(List<Classify> classifyList) {
        classifySecond.clear();
        Collections.reverse(classifyList);
        classifySecond.addAll(classifyList);
        secondAdapter.notifyDataSetChanged();
        closeExpand(classifySecond.size());
    }

    @Override
    public void onFailSecond(String fail) {
        Toast.makeText(baseActivity, fail, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onErrorSecond(String error) {
        Toast.makeText(baseActivity, R.string.network_msg, Toast.LENGTH_SHORT).show();
    }

    /**
     *
     * @param adapterView
     * @param view
     * @param i
     * @param l
     */
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (view.getId()) {
            case R.id.llClassifyFirst:
                presenter.getClassifySecond(classifyFirst.get(i).getId());
                firstAdapter.changeItemSelected(i);
                break;
            case R.id.llItemClassifySecond:
                Intent intent = new Intent(baseActivity, ResultListActivity.class);
                intent.putExtra(IntentCode.EXTRA_KEY_CLASSIFY_ID, classifySecond.get(i).getId());
                startActivity(intent);
                break;
        }
    }

    @Override
    public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
        Intent intent = new Intent(baseActivity, ResultListActivity.class);
        intent.putExtra(IntentCode.EXTRA_KEY_CLASSIFY_ID, classifySecond.get(i).getClassifyList().get(i1).getId());
        startActivity(intent);
        return true;
    }

    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
        if (classifySecond.get(i).getClassifyList().size() == 0 || classifySecond.get(i).getClassifyList() == null) {
            Intent intent = new Intent(baseActivity, ResultListActivity.class);
            intent.putExtra(IntentCode.EXTRA_KEY_CLASSIFY_ID, classifySecond.get(i).getId());
            baseActivity.startActivity(intent);
            return true;//关闭展开
        }
        return false;
    }

    @Override
    public void onGroupExpand(int groupPosition) {
        for (int i = 0, count = classifySecond.size(); i < count; i++) {
            if (groupPosition != i) {
                elvClassifySecond.collapseGroup(i);
            }
        }
    }

    /**
     * 关闭其他分组
     * @param count
     */
    private void closeExpand(int count) {
        for (int i = 0; i < count; i++) {
            elvClassifySecond.collapseGroup(i);
        }
    }

}
